package com.example.homework.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.homework.R;
import com.example.homework.activity.CalculatorActivity;
import com.example.homework.activity.RegisterActivity;
import com.example.homework.activity.SnakeGameActivity;
import com.example.homework.activity.WeatherActivity;
import com.example.homework.adapter.RecommendAdapter;
import com.example.homework.bean.RecommendData;

import java.util.ArrayList;
import java.util.Timer;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RecommendFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RecommendFragment extends Fragment {
    private RecyclerView recyclerViewRecommend;
    private ArrayList<RecommendData> list = new ArrayList<>();
    private Context context;
    private RecommendAdapter recommendAdapter;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public RecommendFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment RecommendFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RecommendFragment newInstance(String param1, String param2) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_recommend, container, false);
        recyclerViewRecommend = view.findViewById(R.id.recommend_recyclerView);
        recyclerViewRecommend.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        initData();
        initView();
        return view;
    }

    private void initView() {
        context = this.getActivity();
        recommendAdapter = new RecommendAdapter(context,list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        recyclerViewRecommend.setLayoutManager(layoutManager);
        recyclerViewRecommend.setAdapter(recommendAdapter);
        recommendAdapter.setMyItemClickListener(((view, position) -> {
            switch (position) {
                case 0:
                    Toast.makeText(context,"您点击了听音乐",Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    Toast.makeText(context,"您点击了看电影",Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(context,"您点击了查天气",Toast.LENGTH_LONG).show();
                    Intent intentWeather = new Intent(context, WeatherActivity.class);
                    context.startActivity(intentWeather);
                    break;
                case 3:
                    Toast.makeText(context,"您点击了计算器",Toast.LENGTH_LONG).show();
                    Intent intentCalculator = new Intent(context, CalculatorActivity.class);
                    context.startActivity(intentCalculator);
                    break;
                case 4:
                    Toast.makeText(context,"您点击了玩游戏", Toast.LENGTH_LONG).show();
                    Intent intentSnakeGame = new Intent(context, SnakeGameActivity.class);
                    context.startActivity(intentSnakeGame);
                    break;
                case 5:
                    Toast.makeText(context,"您点击了去旅游",Toast.LENGTH_LONG).show();
                    break;
                case 6:
                    Toast.makeText(context,"您点击了买买买",Toast.LENGTH_LONG).show();
                    break;
                case 7:
                    Toast.makeText(context,"您点击了做动画",Toast.LENGTH_LONG).show();
                case 8:
                    Toast.makeText(context,"您点击了注册",Toast.LENGTH_LONG).show();
                    Intent intentRegister = new Intent(context, RegisterActivity.class);
                    context.startActivity(intentRegister);
            }
        })
        );
    }

    private void initData() {
        list.add(new RecommendData(R.drawable.music,"听音乐"));
        list.add(new RecommendData(R.drawable.movie,"看电影"));
        list.add(new RecommendData(R.drawable.weather,"查天气"));
        list.add(new RecommendData(R.drawable.jisuanqi,"计算器"));
        list.add(new RecommendData(R.drawable.game,"玩游戏"));
        list.add(new RecommendData(R.drawable.travel,"去旅行"));
        list.add(new RecommendData(R.drawable.shopping,"买买买"));
        list.add(new RecommendData(R.drawable.animation,"做动画"));
        list.add(new RecommendData(R.drawable.re,"可注册"));
    }
}